<?php
declare(strict_types=1);

namespace LowCode\Traits\Vue;

trait HasDefined
{
    public function defineProp(string $key, string|array $value = "String"): static
    {
        if (is_array($value)){
            $type = $value['type'];
            $default = $value['default']??"undefined";
            $required = $value['required']??"false";
            $this->builder->defineProps->add("$key: { type: $type, default: $default, required: $required }");
        }else{
            $this->builder->defineProps->add("$key: $value");
        }
        return $this;
    }

    /**
     * @param array<string,string|array> $props
     * @return $this
     */
    public function defineProps(array $props): static
    {
        foreach ($props as $key=>$prop){
            $this->defineProp($key, $prop);
        }
        return $this;
    }

    /**
     * @param array $emits
     * @return $this
     */
    public function defineEmits(array $emits = []): static
    {
        $this->builder->defineEmits->merge($emits);
        return $this;
    }

    protected function getDefinedHtml(): string
    {
        if ($this->builder->defineProps->isNotEmpty()){
            $values = $this->builder->defineProps->all();
            $props = implode(",",$values);
        }else{
            $props = "";
        }
        if ($this->builder->defineEmits->isNotEmpty()){
            $values = $this->builder->defineEmits->all();
            $emits = implode(",",$values);
        }else{
            $emits = "";
        }
        $this->variables->add("props");
        $this->variables->add("emit");
        if ($this->type === "sfc"){
            return <<<VUE
const props = defineProps({{$props}})
const emit = defineEmits([$emits])
VUE;
        }else{
            return <<<VUE
    props: {{$props}},
    emits: [$emits],
VUE;
        }
    }
}
